<?php
namespace app\wechat\controller;
use app\model\Items;
use app\model\PayLog;
use app\model\Users;
use app\model\Order;
use think\Loader;
use think\Log;
use think\Session;
use think\Config;
use EasyWeChat\Factory;

class OrderController extends BaseController
{
    protected $pay;

    public function _initialize()
    {
        parent::_initialize();
        $this->pay = Factory::payment(Config::get('pay'));
    }

    /**
     * 直接下单
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function pay()
    {
        $item_id = $this->request->param('id');
        $sku_name = $this->request->param('sku_name');
        $sku_id = $this->request->param('sku_id');

        $user = Users::get(['id'=>Session::get('user_id')]);

        $item = Items::get(['id'=>$item_id]);

        $this->assign('sku_name',$sku_name);
        $this->assign('sku_id',$sku_id);
        $this->assign('item',$item);
        $this->assign('user',$user);
        return $this->fetch();
    }

    /**
     * 订单详情
     */
    public function detail(){
        $order_id = $this->request->param('id');
        $order = Order::get(['id'=>$order_id]);

        $this->assign('order',$order);
        return $this->fetch();
    }

    /*****************订单支付相关**********************/
    /**
     * 1.创建订单add()
     * 2.唤起微信支付callPay()
     */
    /**
     * 创建订单
     * @return array
     */
    public function add()
    {
        $order_data = $this->request->post();
        $user_id = Session::get('user_id');

        /**********创建订单**********/
        $order_data['user_id'] = $user_id;
        $rand = builderRand(8);
        $order_data['order_num'] = date('Ymd') . $rand;

        //验证
        $validate = Loader::validate('Order');
        if (!$validate->check($order_data)) {
            return msgReturn(false, $validate->getError());
        }
        $order = new Order();
        $order->data($order_data);
        $order->save();
        if($order->id){
            return msgReturn(true, '创建订单成功',$order->id);
        }else{
            return msgReturn(false, '创建订单失败，请联系客服');
        }
    }

    /**
     * 请求支付订单
     * @return mixed
     */
    public function callPay()
    {
        $order_id = $this->request->post('order_id');
        $order_data = Order::get(['id'=>$order_id]);
        $user_id = Session::get('user_id');

        if($order_data['status'] > 0)
            return msgReturn(false, '已经支付');

        //发起支付请求
        $user = Users::get($user_id);
        if($user['credit'] >= $order_data['money']){
            $user->credit = ($user['credit'] - $order_data['money']);
            $user->save();

            $pay_log = new PayLog([
                'user_id' => $user_id,
                'username' => $user['nickname'],
                'credit' => $order_data['money'],
                'type' => 0,
                'reason' => '兑换商品:'.$order_data['item_name'],
            ]);
            $pay_log->save();

            $order_data->status = 1;
            $order_data->save();

            return msgReturn(true, '兑换成功', '兑换成功');
        }else{
            return msgReturn(false, '您的积分不足', 'error');
        }


    }



}
